package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.constant.dto.BaseFxResponse;
import com.crm.entity.CrmArticle;
import com.crm.entity.CrmSite;
import com.crm.mapper.CrmArticleMapper;
import com.crm.mapper.CrmSiteMapper;
import com.crm.model.bo.ArticlePageBo;
import com.crm.model.dto.BasePageResult;
import com.crm.service.ICrmArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * CRM-文章表 服务实现类
 * </p>
 *
 * @author CRM_SYSTEM
 * @since 2022-03-30
 */
@Service
@Slf4j
public class CrmArticleServiceImpl extends ServiceImpl<CrmArticleMapper, CrmArticle> implements ICrmArticleService {

    @Autowired
    private CrmSiteMapper siteMapper;

    @Override
    public BaseFxResponse<Long> saveArticle(CrmArticle article) {
        this.save(article);
        return BaseFxResponse.success(article.getId()) ;
    }

    @Override
    public BaseFxResponse<CrmArticle> queryById(Long id) {
        CrmArticle crmArticle = this.getById(id);
        return BaseFxResponse.success(crmArticle);
    }

    @Override
    public BaseFxResponse<Long> updateArticle(CrmArticle article) {
        this.updateById(article);
        return BaseFxResponse.success(article.getId());
    }

    @Override
    public BaseFxResponse<Boolean> deleteByArticleId(Long id) {

//        boolean update = this.update(new LambdaUpdateWrapper<CrmArticle>()
//                .set(CrmArticle::getIsDeleted, toString())
//                .eq(CrmArticle::getId, id));
        boolean b = this.removeById(id);
        return BaseFxResponse.success(b);
    }

    @Override
    public BaseFxResponse<BasePageResult<CrmArticle>> queryPage(ArticlePageBo articlePageBo) {
        IPage<CrmArticle> pageParam = new Page<>(articlePageBo.getPageNumber(), articlePageBo.getPageSize());
        IPage<CrmArticle> articleIPage =  baseMapper.queryPage(pageParam,articlePageBo);
        return BaseFxResponse.success(BasePageResult.newInstance(articleIPage));
    }
}
